<template>
  <div class="container">
    <div class="main">
      <div class="pay-title">
        支付总金额
        <span class="pay-price">￥ {{ OrderDetail.price }}</span>
      </div>
      <div class="pay-main">
        <h4>微信支付</h4>
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          class="pay-qrcode"
        >
          <div class="qrcode">
            <!-- 二维码 -->
            <canvas id="qrcode-stage"></canvas>
            <p>请使用微信扫一扫</p>
            <p>扫描二维码支付</p>
          </div>
          <div class="pay-example">
            <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt />
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import QRCode from "qrcode";
export default {
  data() {
    return {
      OrderDetail: {},
      timeId: null
    };
  },
  watch: {
    "$store.state.user": function() {
      //如果是刷新页面，token还没有恢复，需要监听后变化
      this.getOrderDetails();
    }
  },
  mounted() {
    //页面一加载就执行
    if (this.$store.state.user.userInfo.token) {
      this.getOrderDetails();
    }
  },
  destroyed() {
    console.log("支付页面跳转到了其他页面，就销毁轮询");
    clearTimeout(this.timeId);
  },
  methods: {
    CheckPay() {
      this.$axios({
        url: "/airorders/checkpay",
        method: "post",
        data: {
          id: this.$route.query.id,
          nonce_str: this.OrderDetail.price,
          out_trade_no: this.OrderDetail.orderNo
        },
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token
        }
      }).then(res => {
        console.log("支付状态返回", res.data);
        const { trade_state, statusTxt } = res.data;
        if (trade_state == "NOTPAY") {
          this.timeId = setTimeout(() => {
            this.CheckPay();
          }, 3000);
        } else {
          // 如果用户支付成功了，弹出提示告诉用户
          this.$message({
            message: statusTxt,
            type: "success"
          });
          if (trade_state == "SUCCESS") {
            console.log("支付成功");
          }

          if (trade_state == "REFUND") {
            console.log("转入退款");
          }

          if (trade_state == "NOTPAY") {
            console.log("未支付");
          }

          if (trade_state == "CLOSED") {
            console.log("已关闭");
          }

          if (trade_state == "REVOKED") {
            console.log("已撤销（付款码支付）");
          }

          if (trade_state == "USERPAYING") {
            console.log("用户支付中（付款码支付）");
          }

          if (trade_state == "PAYERROR") {
            console.log("支付失败");
          }
        }
      });
    },
    getOrderDetails() {
      this.$axios({
        url: "/airorders/" + this.$route.query.id,
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token
        }
      }).then(res => {
        console.log("订单详情", res);
        this.OrderDetail = res.data;
        const { payInfo } = res.data;
        //生成二维码到canvas中
        const qrcode = document.querySelector("#qrcode-stage");
        QRCode.toCanvas(qrcode, payInfo.code_url, {
          width: 200
        });
        //二维码生成完成，获取支付状态
        this.CheckPay();
      });
    }
  }
};
</script>

<style scoped lang="less">
.container {
  background: #f5f5f5;
  padding: 30px 0;

  .main {
    width: 1000px;
    margin: 0 auto;

    .pay-title {
      text-align: right;
      span {
        font-size: 28px;
        color: orangered;
      }
    }

    .pay-main {
      background: #fff;
      margin-top: 10px;
      border-top: 5px orange solid;
      padding: 30px;

      h4 {
        font-size: 28px;
        font-weight: normal;
        margin-bottom: 10px;
      }

      .pay-qrcode {
        padding: 0 80px;
      }

      .qrcode {
        border: 1px #ddd solid;
        padding: 15px;
        height: fit-content;

        #qrcode-stage {
          width: 200px;
          height: 200px;
          margin-bottom: 10px;
        }

        p {
          line-height: 2;
          text-align: center;
        }
      }
    }
  }
}
</style>
